﻿using PlayListSet.Entity;
using Rug.Osc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace PlayListSet.Comm
{
    public class PlayCMDControl
    {
        private System.Threading.Timer timer;

        public PlayCMDControl()
        {


            timer = new System.Threading.Timer(ThreadingTimer_Elapsed, null, 0, 30);


        }

        public int tempTime = -1;
        private void ThreadingTimer_Elapsed(object state)
        {
            //if(AppControl.Instance.TimeCodePlayContorl.PlaybackState== NAudio.Wave.PlaybackState.Playing)
            // {

            if (AppControl.Instance.TimeCodePlayContorl.PlaybackState == NAudio.Wave.PlaybackState.Playing)
            {
                var curTime = (int)AppControl.Instance.TimeCodePlayContorl.CurrentTime.TotalSeconds;
                if (curTime == tempTime)
                    return;
                tempTime = curTime;
                var type = typeof(PlayCmd);
                foreach (var item in AppControl.Instance.PlayInfoMap.Keys)
                {
                    var list = AppControl.Instance.PlayInfoMap[item];
                    for (var i = 0; i < list.Count; i++)
                    {
                        if (list[i].GetType() == type)
                        {
                            var info = list[i] as PlayCmd;
                            if (((int)info.StartTimeCode) == curTime)
                            {
                                RunPlayCmd(info);
                            }

                        }
                    }
                }


            }


        }
        public void RunPlayCmd(PlayCmd playCmd)
        {
            switch (playCmd.CMDType)
            {
                case 0:
                    AppControl.Instance.TimeCodePlayContorl.Stop();
                    AppControl.Instance.Log($"播控停止{playCmd.Name}执行成功");
                    break;
                case 1:
                    AppControl.Instance.TimeCodePlayContorl.Pause();
                    AppControl.Instance.Log($"播控暂停{playCmd.Name}执行成功");
                    break;
                case 2:
                    var tp = TimeSpan.FromSeconds(playCmd.SeekTime);
                    AppControl.Instance.TimeCodePlayContorl.Seek(tp);
                    AppControl.Instance.Log($"播控跳转{playCmd.Name}执行成功，跳转至{tp.ToString()}");
                    break;
                case 3:
                    Task.Factory.StartNew(() =>
                    {
                        try
                        {
                            RunNet(playCmd);
                            AppControl.Instance.Log($"播控网络指令{playCmd.Name}执行成功");
                        }
                        catch (Exception ex)
                        {
                            AppControl.Instance.Log($"播控网络指令{playCmd.Name}执行失败,{ex.Message}");

                        }
                    });

                    break;
                default:
                    break;
            }
        }
        public static void RunNet(PlayCmd playCmd)
        {
            IPEndPoint iPEndPoint = VCLib.Converts.IPConverts.ConverToIPEndPoint(playCmd.NetInfo);

            byte[] data;
            if (playCmd.DataType == 2)
            {
                data = VCLib.Converts.ByteConverts.ConverToArr(playCmd.Data);
            }
            else if (playCmd.DataType == 1)
            {
                data = Encoding.UTF8.GetBytes(playCmd.Data);
            }
            else
            {
                data = Encoding.ASCII.GetBytes(playCmd.Data);
            }
            var scd = new VCLib.Network.Entity.SocketConnData(iPEndPoint, data, false); ;
            if (playCmd.NetType == 1)
            {
                VCLib.Network.SocketSendHelp.TcpClientSendMessage(scd, 1000);
            }
            else
            {
                VCLib.Network.SocketSendHelp.UdpClientSendMessage(scd);
            }

        }



        ~PlayCMDControl()
        {
            timer.Dispose(); //jishu.Dispose();
        }
    }
}
